/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { expect } from '@jest/globals';
import * as fse from 'fs-extra';
import * as path from 'path';

import { initByStageEmptyApp, initExecuteCommand, runHvigor, } from './common/common';

describe('test generate loader json', () => {
  test.concurrent('test stage mode generate loader json and incremental', async () => {
    await initByStageEmptyApp(async (projectRoot) => {
      const commands = initExecuteCommand({
        mode: 'module',
        product: 'default',
        module: 'entry',
        tasks: 'default@GenerateLoaderJson',
        log: 'i'
      });
      const result = runHvigor(projectRoot, commands);
      expect(result.stdout).toContain('Finished :entry:default@GenerateLoaderJson...');
      expect(result.stdout).toContain('BUILD SUCCESSFUL');

      const executeAgainResult = runHvigor(projectRoot, commands);
      expect(executeAgainResult.stdout).toContain('UP-TO-DATE :entry:default@GenerateLoaderJson...');
      expect(executeAgainResult.stdout).toContain('BUILD SUCCESSFUL');

      const loaderOutputFile = path.resolve(projectRoot,
        'entry/build/default/intermediates/loader/default/loader.json');
      fse.removeSync(loaderOutputFile);

      const executeAgainResultAfterDelete = runHvigor(projectRoot, commands);
      expect(executeAgainResultAfterDelete.stdout).toContain('Finished :entry:default@GenerateLoaderJson...');
      expect(executeAgainResultAfterDelete.stdout).toContain('BUILD SUCCESSFUL');
    });
  });
});
